package com.cn.yunhe.rentcars.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.captcah.BusiLogUtil;
import com.cn.yunhe.rentcars.mapper.BusiLogMapper;
import com.cn.yunhe.rentcars.mapper.ClientUserMapper;
import com.cn.yunhe.rentcars.model.Baclist;
import com.cn.yunhe.rentcars.model.ClientUser;
import com.cn.yunhe.rentcars.model.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 客户管理
 * @author 刘建国
 *
 */



@RestController
public class ClientUserController {
	@Autowired
	private ClientUserMapper clientUserMapper;
	@Autowired
	private BusiLogMapper busiLogMapper;
	
/**
 * 跳转到客户管理页面
 * @return 页面
 *
 */
	@RequestMapping(value="/clients", method=RequestMethod.GET)
	public ModelAndView showPageClientlist(){
		
		return new ModelAndView("client-list");
	}
	
	@RequestMapping(value="/client-add", method=RequestMethod.GET)
	public ModelAndView showAddClient(){
		
		return new ModelAndView("client-add");
	}
	
	@RequestMapping(value="/clients-edit", method=RequestMethod.GET)
	public ModelAndView showEditClient(@RequestParam String uid){
		ClientUser clientUser = clientUserMapper.getClientUserById(uid);
		
		Map<String,ClientUser> modelMap = new HashMap<String,ClientUser>();
		modelMap.put("clientUser", clientUser);
	
		
		return new ModelAndView("client-edit",modelMap);
	}
	
/**
 * 模糊查询客户列表（支持分页）
 * @param name  客户姓名
 * @return
 *
 * @param draw 		[jquery dattable特有的固有的参数，第几次点击查询]
 * * @param start	[jquery dattable特有的固有的参数，从第几条开始查询]
 * @param length	[jquery dattable特有的固有的参数，每页都少条数据]
 * @return
	 */
	@RequestMapping(value="/clientsGetlikecond", method=RequestMethod.GET)
	public Map<String,Object> getClientCond(@RequestParam String name,@RequestParam String mobile,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		//System.out.println("cvv");
		//TODO:把输入参数封装费Map
		Map<String,String> paramsMap = new HashMap<String,String>();
		paramsMap.put("name", name)	;
		paramsMap.put("mobile", mobile);
		
		int pageNum = (start/length)+1;	//获取当前查询第几页
		int pageSize = length;
		//TODO:调用DAO MAPPER数据层传入MAP参数去模糊查询，返回List结构
		PageHelper.startPage(pageNum,pageSize);
		List<ClientUser> client = clientUserMapper.searchClients(paramsMap);
		PageInfo<ClientUser> page = new PageInfo<ClientUser>(client);
		
		Map<String,Object> rsMap = new HashMap<String,Object>();
		
		rsMap.put("draw",draw);
	    rsMap.put("data", page.getList());	      
	    rsMap.put("recordsTotal", page.getTotal());
	    rsMap.put("recordsFiltered", page.getTotal());
		return rsMap;
	}
	
	/**
	 * 添加用户
	 * @param clientUser
	 */
	@RequestMapping(value="/addClient1", method=RequestMethod.POST)
	public void addClient(@RequestBody ClientUser clientUser,HttpServletRequest request){
		clientUser.setUid(UUID.randomUUID().toString());
		//System.out.println("sf");
		clientUserMapper.addClient1(clientUser);
		//添加日志信息
        Map<String,Object> busMap = new HashMap<String,Object>();
        busMap.put("logid", UUID.randomUUID().toString());
        busMap.put("businame", "添加客户信息");
        busMap.put("busitype", "添加");
        busMap.put("busiconent","添加客户信息");
        BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
		
	
	}
	/**
	 * 根据ID删除用户
	 * @param uid
	 */
	@RequestMapping(value="/delClient", method=RequestMethod.POST)
	public void delClient(@RequestParam String uid,HttpServletRequest request){ 
		clientUserMapper.delClient(uid);
		//添加日志信息
        Map<String,Object> busMap = new HashMap<String,Object>();
        busMap.put("logid", UUID.randomUUID().toString());
        busMap.put("businame", "删除客户信息");
        busMap.put("busitype", "删除");
        busMap.put("busiconent","删除客户信息");
        BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
	}
	
	
	
	/**
	 * 修改用户
	 * 
	 * @param uid
	 */
	@RequestMapping(value="/editClient/{uid}", method=RequestMethod.PUT)
	public void editClient(@PathVariable("uid") String uid,@RequestBody ClientUser clientUser,HttpServletRequest request){
		//System.out.print("shifoushuchu");
		clientUser.setUid(uid);
		clientUserMapper.editClient1(clientUser);
		//添加日志信息
        Map<String,Object> busMap = new HashMap<String,Object>();
        busMap.put("logid", UUID.randomUUID().toString());
        busMap.put("businame", "修改客户信息");
        busMap.put("busitype", "修改");
        busMap.put("busiconent","修改客户信息");
        BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
	}
	
	
	/**
	 * 编辑用户状态
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/setClientState", method=RequestMethod.POST)
	public void handleClientState(@RequestParam String uid,@RequestParam  int isabled){ 
		ClientUser clientUser = new ClientUser();
		clientUser.setUid(uid);
		
		clientUserMapper.setClientState(clientUser);
	}
	
	
	/**
	 * 批量删除用户信息
	 * 
	 * @param uid
	 * @param 
	 */
	@RequestMapping(value="/delClientUsers", method=RequestMethod.POST)
	public void delClientUsers(@RequestParam String clientUsersuid,HttpServletRequest request){ 
		
		List<String> uidList = Arrays.asList(clientUsersuid.split(","));
		
		
		clientUserMapper.delClients(uidList);
		//添加日志信息
        Map<String,Object> busMap = new HashMap<String,Object>();
        busMap.put("logid", UUID.randomUUID().toString());
        busMap.put("businame", "批量删除客户信息");
        busMap.put("busitype", "批量删除");
        busMap.put("busiconent","批量删除客户信息");
        BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
	
	}
	

	
}